﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Stock.Core.MongoDB;
using Newtonsoft.Json;

namespace Stock.Core.Common
{
    public class LogModel:MongoEntity<LogModel>
    {

        public DateTime ExecuteStartTime { get; set; }
        public string HttpRequestHeaders { get; set; }
        public string HttpMethod { get; set; }
        public string ActionName { get; set; }

        /// <summary>
        /// ControllerName
        /// </summary>
        /// <value>The name of the controller.</value>
        public string ControllerName { get; set; }

        /// <summary>
        /// 执行结束时间
        /// </summary>
        /// <value>The execute end time.</value>
        public DateTime ExecuteEndTime { get; set; }

        /// <summary>
        /// 耗时
        /// </summary>
        /// <value>The executed time.</value>
        public int ExecutedTime { get; set; }

        /// <summary>
        /// 请求参数
        /// </summary>
        /// <value>The action parameters.</value>
        public string ActionParams { get; set; }

        /// <summary>
        /// 请求长度
        /// </summary>
        /// <value>The length of the content.</value>
        public string ContentLength { get; set; }

        /// <summary>
        /// 信息
        /// </summary>
        /// <value>The message.</value>
        public string Message { get; set; }
        /// <summary>
        /// 日志类型：错误，调试
        /// </summary>
        /// <value>The type of the log.</value>
        public string LogType { get; set; }

        /// <summary>
        /// Gets or sets the stack trace.
        /// </summary>
        /// <value>The stack trace.</value>
        public string StackTrace { get; set; }

        public void setParams(IDictionary<string, object> p)
        {
            string msg = string.Empty;
            p.ToList().ForEach(item =>
            {
                msg += item.Key + ":" + JsonConvert.SerializeObject(item.Value) + ",";
            });
            this.ActionParams = msg;
        }

        public string GetLoginfo()
        {
            string Msg = $"请求【{this.ActionName}】完成：" +
                $"\r\nAction参数：{this.ActionParams}" +
                                          $"\r\nHttp请求头:{this.HttpRequestHeaders}" +
                                          $"\r\n客户端IP：{4}," +
                                          $"\r\nHttpMethod:{this.HttpMethod}," +
                $"\r\nExecutedTime:{(this.ExecuteEndTime - this.ExecuteStartTime).Milliseconds.ToString()}ms," +
                $"\r\n 信息:{this.Message}";
            return Msg;
        }
    }

}